package com.mf.basic.services;

import com.mf.basic.entity.DtoDicCatalog;
import com.mf.basic.mapper.MapperDicCatalog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by Luke on 2025/3/8 17:03.
 */
@Service
@Transactional
public class ServiceDicCatalog extends ServiceImpl<MapperDicCatalog, DtoDicCatalog> implements BaseService<DtoDicCatalog>{
    @Override
    public LambdaQueryWrapper<DtoDicCatalog> fillParams(Map<String, Object> params) {
        return null;
    }

    /**
     * 查询字典目录树
     * @return Map<String, List<DtoDicCatalog>>
     */
    public Map<String, List<DtoDicCatalog>> findTree(){
        // 查询所有字典目录(除系统目录外)
        LambdaQueryWrapper<DtoDicCatalog> qw = loadQw()
                .eq(DtoDicCatalog::getFlagSystem, 0)
                .eq(DtoDicCatalog::getStatus, 0);
        List<DtoDicCatalog> list = list(qw);
        return list.stream().collect(Collectors.groupingBy(DtoDicCatalog::getDicType));
    }

    /**
     * 检查是否为系统内置字典目录
     * @param catalogId 字典目录ID
     * @return DtoDicCatalog
     */
    public DtoDicCatalog checkSystem(long catalogId){
        DtoDicCatalog catalog = findCheckById((int) catalogId, true);
        Assert.state(catalog.getFlagSystem() == 0, "当前字典目录为系统内置, 无法执行操作");
        Assert.state(catalog.getStatus() == 0, "当前字典目录已被禁用, 无法执行操作");
        return catalog;
    }

}
